module.exports = (EdtApi) => {
    "use strict";
    let chatId = "", count = 0;
    function isGroup() {
        return false;
    }
    async function getChatId(times = 0) {
        let chatTitleEl = document.querySelector("#chat-right header span.yes-select>span:first-child");
        let id = chatTitleEl?.textContent || '';
        if (id && id != chatId) return id;
        await EdtApi.sleep(50);
        if (times > 10) return id;
        times++;
        return getChatId(times);
    }
    return {
        config: {
            friendList: 'main section aside>ul',
            chat: '#chat-right',
            input: '#chat-right>form #inputChat',
            msgBox: '#screen-chat',
            inMsg: 'li span.message div.left-message:not(.trans-inited)',
            outMsg: 'li span.message div.right-message:not(.trans-inited)',
            sendBtn: '#send-button',
            setting: "#inputChat",
            tips: {
                in: '#chat-right header>span:first-child',
                out: '#chat-right>form'
            },
            crm: {
                // listener: '#LeftColumn-main',
                top: '#chat-right header>span:first-child>div:first-child',
                left: ""
            },
            placeholder: {
                selector: "#chat-right>form #inputChat",
                attr: "placeholder"
            },
            autoCount: 20,
            inputType: "insert",
        },
        getUnread() {
            count = 0;
            const els = document.querySelectorAll('main section aside>ul>li span.v-badge__wrapper>span.v-badge__badge');
            els?.forEach((el) => {
                count += EdtApi.safeParseInt(el.textContent || "0");
            });
            return count;
        },
        getChatInfo() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                resolve({ id: chatId, isGroup: isGroup() });
            });
        },
        getText(msgBox) {
            if (!msgBox) return "";
            let value = "";
            try {
                msgBox.childNodes?.forEach((child) => {
                    if (child.nodeName == "#text") {
                        value += child.textContent;
                    } else if (child.nodeName == "DIV") {
                        value += "\n" + (child.textContent || "");
                    } else if (child.nodeName == "BR") {
                        value += "\n";
                    }
                });
            } catch (error) {
                console.error(error);
            }
            console.log(value);
            return value;
        },
        getIptMsg(inputEl) {
            if (!inputEl) return "";
            let value = "";
            try {
                inputEl.childNodes?.forEach((child) => {
                    if (child.nodeName == "#text") {
                        value += child.textContent || "";
                    } else if (child.nodeName == "BR") {
                        value += "\n";
                    }
                });
            } catch (error) {
                console.error(error);
            }
            return value;
        },
        getUserInfo() {
            try {
                let accountEl = document.querySelector("header>figure span.title.yes-select");
                let userName = accountEl?.textContent || "";
                let account = userName;
                return {
                    avatar: "",
                    userName,
                    account
                };
            } catch (error) {
                return null;
            }
        }
    }
};